package com.nidu.demo.web.system;

import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.nidu.demo.auth.dto.*;
import com.nidu.demo.auth.api.SysOAuth2Service;
import com.nidu.demo.security.config.SecurityProperties;
import com.nidu.demo.security.util.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@Tag(name = "管理后台 - 认证")
@RestController
@RequestMapping("/system/auth")
@Validated
@RequiredArgsConstructor
public class SysAuthController {

    private final SysOAuth2Service service;

    private final SecurityProperties securityProperties;

    @PostMapping("/login")
    @PermitAll
    @Operation(summary = "使用账号密码登录")
    public SingleResponse<AuthLoginCO> login(@RequestBody @Valid AuthLoginCmd cmd) {
        return service.login(cmd);
    }

    @PostMapping("/logout")
    @Operation(summary = "登出系统")
    public Response logout(HttpServletRequest request) {
        String token = SecurityUtils.getTokenFromRequest(request, securityProperties.getTokenHeader());
        return service.logout(token);
    }

    @PostMapping("/refresh-token")
    @Operation(summary = "刷新令牌")
    @Parameter(name = "refreshToken", description = "刷新令牌", required = true)
    public SingleResponse<AuthLoginCO> refreshToken(@RequestParam("refreshToken") String refreshToken) {
        return service.refreshToken(refreshToken);
    }

    @PostMapping("/register")
    @PermitAll
    @Operation(summary = "注册用户")
    public SingleResponse<AuthLoginCO> register(@RequestBody @Valid AuthRegisterCmd cmd) {
        return service.register(cmd);
    }

    @PostMapping("/sms-login")
    @PermitAll
    @Operation(summary = "使用短信验证码登录")
    public SingleResponse<AuthLoginCO> smsLogin(@RequestBody @Valid AuthSmsLoginCmd cmd) {
        return service.smsLogin(cmd);
    }

    @PostMapping("/send-sms-code")
    @PermitAll
    @Operation(summary = "发送手机验证码")
    public Response sendLoginSmsCode(@RequestBody @Valid AuthSmsSendCmd cmd) {
        return service.sendSmsCode(cmd);
    }

//    @GetMapping("/social-auth-redirect")
//    @PermitAll
//    @Operation(summary = "社交授权的跳转")
//    @Parameters({
//            @Parameter(name = "type", description = "社交类型", required = true),
//            @Parameter(name = "redirectUri", description = "回调路径")
//    })
//    public SingleResponse<String> socialLogin(@RequestParam("type") Integer type,
//                                            @RequestParam("redirectUri") String redirectUri) {
//        return socialClientService.getAuthorizeUrl(
//                type, UserTypeEnum.ADMIN.getValue(), redirectUri);
//    }

    @PostMapping("/social-login")
    @PermitAll
    @Operation(summary = "社交快捷登录，使用 code 授权码", description = "适合未登录的用户，但是社交账号已绑定用户")
    public SingleResponse<AuthLoginCO> socialQuickLogin(@RequestBody @Valid AuthSocialLoginCmd cmd) {
        return service.socialLogin(cmd);
    }
}